package com.quectel.emp.service.deptperson;

import com.google.common.collect.Sets;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.module.dept.dto.DeptPersonAccessResourceDto;
import com.quectel.core.module.dept.dto.DeptPersonRoleDto;
import com.quectel.core.module.dept.dto.DeptPersonRoleRelationDto;
import com.quectel.core.module.dept.dto.DeptPersonRoleResourceDto;
import com.quectel.core.module.dept.service.DeptPersonAccessResourceService;
import com.quectel.core.module.dept.service.DeptPersonRoleRelationService;
import com.quectel.core.module.dept.service.DeptPersonRoleResourceService;
import com.quectel.core.module.dept.service.DeptPersonRoleService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 在这里编写说明
 *
 * @author: caolu
 * @email: louis.cao@quectel.com
 * @date: 2022-07-27 11:39:00
 */
@Component
public class ApiDeptPersonService {
    @DubboReference
    private DeptPersonAccessResourceService personAccessResourceService;
    @DubboReference
    private DeptPersonRoleRelationService deptPersonRoleRelationService;
    @DubboReference
    private DeptPersonRoleResourceService deptPersonRoleResourceService;
    @DubboReference
    private DeptPersonRoleService deptPersonRoleService;


    /**
     * 查询人员可访问资源集合
     *
     * @param id
     * @return
     */
    public Set<String> queryResourcesById(Long id) {
        Set<String> resources = Sets.newHashSet();
        //查询所有的资源
        List<DeptPersonAccessResourceDto> allResource = personAccessResourceService.queryAllAvailableResource();

        //插入必须选资源
        resources.addAll(allResource.stream().filter(v -> Objects.equals(v.getOptional(), SystemConstants.YES))
                .filter(v -> SystemConstants.NORMAL.equals(v.getStatus()))
                .map(DeptPersonAccessResourceDto::getPermission).collect(Collectors.toSet()));

        //查询人员角色
        List<DeptPersonRoleRelationDto> personRoleList = deptPersonRoleRelationService.selectByPersonId(id);
        if (CollectionUtils.isEmpty(personRoleList)) {
            return resources;
        }
        for (DeptPersonRoleRelationDto deptPersonRoleRelationDto : personRoleList) {
            DeptPersonRoleDto deptPersonRoleDto = deptPersonRoleService.selectById(deptPersonRoleRelationDto.getDeptRoleId());
            if (deptPersonRoleDto == null || SystemConstants.ABNORMAL.equals(deptPersonRoleDto.getStatus())) {
                continue;
            }
            //查询角色与资源关系
            List<DeptPersonRoleResourceDto> roleResources = deptPersonRoleResourceService.selectByRoleId(deptPersonRoleRelationDto.getDeptRoleId());
            if (CollectionUtils.isEmpty(roleResources)) {
                continue;
            }
            for (DeptPersonRoleResourceDto roleResource : roleResources) {
                //根据资源id查找资源实体
                DeptPersonAccessResourceDto accessResource = allResource.stream().filter(v -> Objects.equals(roleResource.getAccessResourceId(), v.getId()))
                        .findAny().orElse(null);
                if (accessResource != null) {
                    resources.add(accessResource.getPermission());
                }
            }
        }
        return resources;

    }

}
